package com.jeesite.modules.task.dceconomicindicator.service;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.task.dceconomicindicator.entity.DcEconomicIndicator;
import com.jeesite.modules.task.dceconomicindicator.dao.DcEconomicIndicatorDao;
import org.springframework.web.multipart.MultipartFile;

/**
 * 经济指标Service
 * @author xhc
 * @version 2022-10-20
 */
@Service
@Transactional(readOnly=false)
public class DcEconomicIndicatorService extends CrudService<DcEconomicIndicatorDao, DcEconomicIndicator> {
	
	/**
	 * 获取单条数据
	 * @param dcEconomicIndicator
	 * @return
	 */
	@Override
	public DcEconomicIndicator get(DcEconomicIndicator dcEconomicIndicator) {
		return super.get(dcEconomicIndicator);
	}
	
	/**
	 * 查询分页数据
	 * @param dcEconomicIndicator 查询条件
	 * @param dcEconomicIndicator.page 分页对象
	 * @return
	 */
	@Override
	public Page<DcEconomicIndicator> findPage(DcEconomicIndicator dcEconomicIndicator) {
		return super.findPage(dcEconomicIndicator);
	}
	
	/**
	 * 查询列表数据
	 * @param dcEconomicIndicator
	 * @return
	 */
	@Override
	public List<DcEconomicIndicator> findList(DcEconomicIndicator dcEconomicIndicator) {
		return super.findList(dcEconomicIndicator);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param dcEconomicIndicator
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DcEconomicIndicator dcEconomicIndicator) {
		super.save(dcEconomicIndicator);
	}
	
	/**
	 * 更新状态
	 * @param dcEconomicIndicator
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DcEconomicIndicator dcEconomicIndicator) {
		super.updateStatus(dcEconomicIndicator);
	}
	
	/**
	 * 删除数据
	 * @param dcEconomicIndicator
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcEconomicIndicator dcEconomicIndicator) {
		super.delete(dcEconomicIndicator);
	}
	@Transactional(readOnly=false)
    public void infoImport(MultipartFile file) throws IOException {
		// 读取excel中的数据到集合中
		InputStream inputStream = file.getInputStream();
		List<List<JSONObject>> listMerges  = ExcelUtils.readMergeExcel(file.getOriginalFilename(), inputStream,0,0,0);
		int columnSize =0;
		for(int i=0; i<listMerges.size(); i++){
			System.out.println(listMerges.get(i).toString());
			if(listMerges.get(i).toString().contains("芜湖市")){
				columnSize = listMerges.get(i).size();
			}
		}

		List<Map<String,String>> data = new ArrayList<>();
		Map map_wh =null;
		Map map_column =null;
		Map map_column_two =null;
		for(int i=1; i<listMerges.size(); i++){
			TreeMap<String,String> map_data= new TreeMap<String,String>(new Comparator<String>(){
				public int compare(String a,String b){
					String a_=a.split("column")[1];
					String b_=b.split("column")[1];
					return Integer.parseInt(a_)-Integer.parseInt(b_);
				}
			});
			for(int a=0;a<columnSize;a++){
				String column ="column";
				String cellValue = listMerges.get(i).get(a).getString("cellValue");
				if(StringUtils.isBlank(cellValue)){
					throw new ServiceException("不允许有单元格值为空");
				}
				map_data.put(column+a,cellValue);
			}
			if(listMerges.get(i).toString().contains("芜湖市")){
				map_wh =map_data;
			}
			if(i==1){
				map_column =map_data;
			}
			if(i==2){
				map_column_two =map_data;
			}
			data.add(map_data);
		}
//		for(int a=0;a<columnSize;a++){
//			String column ="column";
//			List list = new ArrayList();
//			for(int k=1; k<listMerges.size(); k++){
//				System.out.println(listMerges.get(k).toString());
//				for(int x=0; x<listMerges.get(k).size(); x++){
//					if(a==x){
//						list.add(listMerges.get(k).get(x).getString("cellValue"));
//					}
//				}
//
//			}
//			map_data.put(column+a,list);
//		}
//		List<Map<String,String>> data_title = new ArrayList<>();
//
//		Set<String> keys = map_data.keySet();
//		for (String key:keys) {
//			Map map =new HashMap<>();
//			map.put(key,(map_data.get(key)).get(0));
//			data_title.add(map);
//		}


		//title
		JSONObject json_title =listMerges.get(0).get(0);
		String title =  json_title.getString("cellValue");
		String jsonData = JSONObject.toJSONString(data);

		String jsonWhData = JSONObject.toJSONString(map_wh);
		String jsonColumnData = JSONObject.toJSONString(map_column);
		String jsonColumnTwoData = JSONObject.toJSONString(map_column_two);
		DcEconomicIndicator dcEconomicIndicator = new DcEconomicIndicator();
		dcEconomicIndicator.setJsonData(jsonData);
		dcEconomicIndicator.setJsonColumnData(jsonColumnData);
		dcEconomicIndicator.setJsonColumnTwoData(jsonColumnTwoData);
		dcEconomicIndicator.setJsonWhData(jsonWhData);
		dcEconomicIndicator.setTitle(title);
		super.save(dcEconomicIndicator);
    }

	public Map getProvinceMajorRank(String title) {
		Map map = new HashMap();
		DcEconomicIndicator dcEconomicIndicator = new DcEconomicIndicator();
		dcEconomicIndicator.setTitle(title);
		List<DcEconomicIndicator> list = dao.findList(dcEconomicIndicator);
		if(list!=null){
			DcEconomicIndicator dcEconomicIndicator1 = list.get(0);
			String jsonWhData = dcEconomicIndicator1.getJsonWhData();
			String jsonColumnData = dcEconomicIndicator1.getJsonColumnData();
			Map mapColumn = JSONObject.parseObject(jsonColumnData,Map.class);
			Map mapWhData = JSONObject.parseObject(jsonWhData,Map.class);
			// 定义接收集合
			List<String> xAxisData = new ArrayList();
			List<Integer> seriesData = new ArrayList();
			List<Integer> tooltipData = new ArrayList();
			for(int a=4;a<mapColumn.keySet().size();a++){
				if(a%2==0){
					xAxisData.add(mapColumn.get("column"+a).toString());
					String num = mapWhData.get("column" + a).toString();
					String[] split = num.split("\\.");
					tooltipData.add(Integer.parseInt(split[0]));

				}

			}
			// 处理柱状图展示高度问题
			Integer tempInt = 0;
			// 获取最大值
			for (Integer item : tooltipData) {
				if (item > tempInt){
					tempInt = item;
				}
			}
			// 加一，保证最小值至少为1
			tempInt+=1;
			// 倒序解决高度问题
			for (Integer item : tooltipData) {
				seriesData.add(tempInt - item);
			}
			map.put("xAxisData",xAxisData);
			map.put("seriesData",seriesData);
			map.put("tooltipData",tooltipData);
		}

			return map;

	}

    public Map getScore() {
		Map map = new HashMap();

		List<Map<String,Object>> list = dao.findScore();
		if(list!=null){
			List<String> xAxisData = new ArrayList();
			List<BigDecimal> seriesData = new ArrayList();
			List<BigDecimal> tooltipData = new ArrayList();
			for(Map map2 : list){
				xAxisData.add((String)map2.get("leader_org_name"));
				tooltipData.add((BigDecimal)map2.get("score"));
				seriesData.add((BigDecimal)map2.get("score"));
			}
			map.put("xAxisData",xAxisData);
			map.put("seriesData",seriesData);
			map.put("tooltipData",tooltipData);
		}


			return map;

}

	/**
	 * 导入重新逻辑
	 * @param file
	 * @throws IOException
	 */
	@Transactional(readOnly=false)
	public void infoImport2(MultipartFile file,String selectMonth,String selectYear) throws IOException {
		// 读取excel中的数据到集合中
		InputStream inputStream = file.getInputStream();
		List<List<JSONObject>> listMerges  = ExcelUtils.readMergeExcel(file.getOriginalFilename(), inputStream,0,0,0);
		int columnSize =0;
		for(int i=0; i<listMerges.size(); i++){
			System.out.println(listMerges.get(i).toString());
			if(listMerges.get(i).toString().contains("芜湖市")){
				columnSize = listMerges.get(i).size();
			}
		}

		List<Map<String,String>> data = new ArrayList<>();
		Map map_wh =null;
		Map map_column =null;
		Map map_column_two =null;
		for(int i=0; i<listMerges.size(); i++){
			TreeMap<String,String> map_data= new TreeMap<String,String>(new Comparator<String>(){
				public int compare(String a,String b){
					String a_=a.split("column")[1];
					String b_=b.split("column")[1];
					return Integer.parseInt(a_)-Integer.parseInt(b_);
				}
			});
			for(int a=0;a<columnSize;a++){
				String column ="column";
				String cellValue = listMerges.get(i).get(a).getString("cellValue");
				if(StringUtils.isBlank(cellValue)){
					throw new ServiceException("不允许有单元格值为空");
				}
				map_data.put(column+a,cellValue);
			}
			if(listMerges.get(i).toString().contains("芜湖市")){
				map_wh =map_data;
			}
			if(i==0){
				map_column =map_data;
			}
			if(i==1){
				map_column_two =map_data;
			}
			data.add(map_data);
		}

		TreeMap<String,Object> map_data= new TreeMap<String,Object>(new Comparator<String>(){
			public int compare(String a,String b){
				String a_=a.split("column")[1];
				String b_=b.split("column")[1];
				return Integer.parseInt(a_)-Integer.parseInt(b_);
			}
		});
		for(int a=0;a<columnSize;a++){
			String column ="column";
			List list = new ArrayList();
			for(int k=2; k<listMerges.size(); k++){
				System.out.println(listMerges.get(k).toString());
				for(int x=0; x<listMerges.get(k).size(); x++){
					if(a==x){
						list.add(listMerges.get(k).get(x).getString("cellValue"));
					}
				}

			}
			map_data.put(column+a,list);
		}

		map_column.remove("column0");
//		map_column.remove("column1");
//		map_column.remove("column2");
		LinkedHashMap map_values = new LinkedHashMap();
		Set<String> set = (Set<String>)map_column.keySet();

		for(String s:set){
			String o =(String ) map_column.get(s);
			Map three = new HashMap();
			three.put("area",map_data.get("column0"));
			if(map_values.containsKey(o)){
				three.put("point",((Map)map_values.get(o)).get("point"));
				three.put("rank",map_data.get(s));

			}else{
				three.put("point",map_data.get(s));
			}
			map_values.put(o,three);

		}


		String jsonData = JSONObject.toJSONString(data);

		String jsonWhData = JSONObject.toJSONString(map_wh);
		String jsonColumnData = JSONObject.toJSONString(map_column);
		String jsonColumnTwoData = JSONObject.toJSONString(map_column_two);
		String jsonColumnDataChart = JSONObject.toJSONString(map_values);
		DcEconomicIndicator dcEconomicIndicator = new DcEconomicIndicator();
		dcEconomicIndicator.setSelectMonth(selectMonth);
		dcEconomicIndicator.setSelectYear(selectYear);
		dcEconomicIndicator.setImportType("1");
		List<DcEconomicIndicator> list = dao.findList(dcEconomicIndicator);
		if(list!=null && list.size()>0){
			dcEconomicIndicator =list.get(0);
		}
		dcEconomicIndicator.setJsonData(jsonData);
		dcEconomicIndicator.setJsonColumnData(jsonColumnData);
		dcEconomicIndicator.setJsonColumnTwoData(jsonColumnTwoData);
		dcEconomicIndicator.setJsonWhData(jsonWhData);
		dcEconomicIndicator.setTitle("");
		dcEconomicIndicator.setJsonColumnDataChart(jsonColumnDataChart);
		dcEconomicIndicator.setImportType("1");
		super.save(dcEconomicIndicator);
	}


	public Map getRank1(String leftParam, String selectYear, String selectMonth, String importType) {
		DcEconomicIndicator dcEconomicIndicator =dao.getRank(selectYear,selectMonth,importType);
		Map map1 =null;
		if(dcEconomicIndicator!=null){
			Map map = JSONObject.parseObject(dcEconomicIndicator.getJsonColumnDataChart(),Map.class);
			map1 =(Map)map.get(leftParam);
		}
		if(map1!=null && StringUtils.equals(dcEconomicIndicator.getImportType(),"3")){
			JSONArray  point = (JSONArray) map1.get("point");
			JSONArray  province_ab = (JSONArray) map1.get("province_ab");
			map1.put("point",getFourString(point));
			map1.put("province_ab",getFourString(province_ab));
		}

		return map1;
	}
		String [] getFourString(JSONArray  array){
			String [] dd = new String[array.size()];
			for(int i=0;i<array.size();i++){
				double v = Double.parseDouble(array.getString(i));
				dd[i] = new Formatter().format("%.4f",v ).toString();
			}
			return dd;
		}
	public Set<String> getColumn(String selectYear, String selectMonth, String importType) {
		DcEconomicIndicator dcEconomicIndicator =dao.getRank(selectYear,selectMonth,importType);
		Set<String> set =null;
		if(dcEconomicIndicator!=null) {
			LinkedHashMap map = JSONObject.parseObject(dcEconomicIndicator.getJsonColumnDataChart(), LinkedHashMap.class);
			 set = map.keySet();
		}
		return set;
	}

	/**
	 * 导入重新逻辑
	 * @param file
	 * @throws IOException
	 */
	@Transactional(readOnly=false)
	public void infoImport4(MultipartFile file,String selectMonth,String selectYear) throws IOException {
		// 读取excel中的数据到集合中
		InputStream inputStream = file.getInputStream();
		List<List<JSONObject>> listMerges  = ExcelUtils.readMergeExcel(file.getOriginalFilename(), inputStream,0,0,0);
		int columnSize =0;
		for(int i=0; i<listMerges.size(); i++){
			System.out.println(listMerges.get(i).toString());
			if(listMerges.get(i).toString().contains("芜湖市")){
				columnSize = listMerges.get(i).size();
			}
		}

		List<Map<String,String>> data = new ArrayList<>();
		Map map_wh =null;
		Map map_column =null;
		Map map_column_two =null;
		for(int i=0; i<listMerges.size(); i++){
			TreeMap<String,String> map_data= new TreeMap<String,String>(new Comparator<String>(){
				public int compare(String a,String b){
					String a_=a.split("column")[1];
					String b_=b.split("column")[1];
					return Integer.parseInt(a_)-Integer.parseInt(b_);
				}
			});
			for(int a=0;a<columnSize;a++){
				String column ="column";
				String cellValue = listMerges.get(i).get(a).getString("cellValue");
				if(StringUtils.isBlank(cellValue)&&i!=2){
					throw new ServiceException("不允许有单元格值为空");
				}
				map_data.put(column+a,cellValue);
			}
			if(listMerges.get(i).toString().contains("芜湖市")){
				map_wh =map_data;
			}
			if(i==0){
				map_column =map_data;
			}
			if(i==1){
				map_column_two =map_data;
			}
			data.add(map_data);
		}

		TreeMap<String,Object> map_data= new TreeMap<String,Object>(new Comparator<String>(){
			public int compare(String a,String b){
				String a_=a.split("column")[1];
				String b_=b.split("column")[1];
				return Integer.parseInt(a_)-Integer.parseInt(b_);
			}
		});
		for(int a=0;a<columnSize;a++){
			String column ="column";
			List list = new ArrayList();
			for(int k=3; k<listMerges.size(); k++){
				System.out.println(listMerges.get(k).toString());
				for(int x=0; x<listMerges.get(k).size(); x++){
					if(a==x){
						list.add(listMerges.get(k).get(x).getString("cellValue"));
					}
				}

			}
			map_data.put(column+a,list);
		}

		map_column.remove("column0");
		LinkedHashMap map_values_ab = new LinkedHashMap();
		Set<String> set = (Set<String>)map_column.keySet();

		List<String> set_ab = new ArrayList<>();

		for(String s:set){
			String suff_num = s.split("column")[1];
			int num = Integer.parseInt(suff_num);
			if(num % 3 == 1){
				set_ab.add(s);
			}
			if(num % 3 == 2){
				set_ab.add(s);
			}
			if(num % 3 == 0){
				set_ab.add(s);
			}
		}

		for(String s:set_ab){
			String o =(String ) map_column.get(s);
			Map three = new HashMap();
			three.put("area",map_data.get("column0"));
			if(map_values_ab.containsKey(o)){
				if(((Map)map_values_ab.get(o)).containsKey("province_ab")){
					three.put("point",((Map)map_values_ab.get(o)).get("point"));
					three.put("province_ab",((Map)map_values_ab.get(o)).get("province_ab"));
					three.put("rank",map_data.get(s));
					map_values_ab.put(o,three);
					continue;
				}
				three.put("point",((Map)map_values_ab.get(o)).get("point"));
				three.put("province_ab",map_data.get(s));

			}else{
				three.put("point",map_data.get(s));
			}
			map_values_ab.put(o,three);

		}

		String jsonData = JSONObject.toJSONString(data);

		String jsonWhData = JSONObject.toJSONString(map_wh);
		String jsonColumnData = JSONObject.toJSONString(map_column);
		String jsonColumnTwoData = JSONObject.toJSONString(map_column_two);
		String jsonColumnDataChart = JSONObject.toJSONString(map_values_ab);
		DcEconomicIndicator dcEconomicIndicator = new DcEconomicIndicator();
		dcEconomicIndicator.setSelectMonth(selectMonth);
		dcEconomicIndicator.setSelectYear(selectYear);
		dcEconomicIndicator.setImportType("3");
		List<DcEconomicIndicator> list = dao.findList(dcEconomicIndicator);
		if(list!=null && list.size()>0){
			dcEconomicIndicator =list.get(0);
		}
		dcEconomicIndicator.setJsonData(jsonData);
		dcEconomicIndicator.setJsonColumnData(jsonColumnData);
		dcEconomicIndicator.setJsonColumnTwoData(jsonColumnTwoData);
		dcEconomicIndicator.setJsonWhData(jsonWhData);
		dcEconomicIndicator.setTitle("");
		dcEconomicIndicator.setJsonColumnDataChart(jsonColumnDataChart);

		dcEconomicIndicator.setImportType("3");
		super.save(dcEconomicIndicator);
	}



	/**
	 * 导入重新逻辑
	 * @param file
	 * @throws IOException
	 */
	@Transactional(readOnly=false)
	public void infoImport3(MultipartFile file,String selectMonth,String selectYear) throws IOException {
		// 读取excel中的数据到集合中
		InputStream inputStream = file.getInputStream();
		List<List<JSONObject>> listMerges  = ExcelUtils.readMergeExcel(file.getOriginalFilename(), inputStream,0,0,0);
		int columnSize =0;
		for(int i=0; i<listMerges.size(); i++){
			System.out.println(listMerges.get(i).toString());
			if(listMerges.get(i).toString().contains("芜湖市")){
				columnSize = listMerges.get(i).size();
			}
		}

		List<Map<String,String>> data = new ArrayList<>();
		Map map_wh =null;
		Map map_column =null;
		Map map_column_two =null;
		for(int i=0; i<listMerges.size(); i++){
			TreeMap<String,String> map_data= new TreeMap<String,String>(new Comparator<String>(){
				public int compare(String a,String b){
					String a_=a.split("column")[1];
					String b_=b.split("column")[1];
					return Integer.parseInt(a_)-Integer.parseInt(b_);
				}
			});
			for(int a=0;a<columnSize;a++){
				String column ="column";
				String cellValue = listMerges.get(i).get(a).getString("cellValue");
				if(StringUtils.isBlank(cellValue)&&i!=2){
					throw new ServiceException("不允许有单元格值为空");
				}
				map_data.put(column+a,cellValue);
			}
			if(listMerges.get(i).toString().contains("芜湖市")){
				map_wh =map_data;
			}
			if(i==0){
				map_column =map_data;
			}
			if(i==1){
				map_column_two =map_data;
			}
			data.add(map_data);
		}

		TreeMap<String,Object> map_data= new TreeMap<String,Object>(new Comparator<String>(){
			public int compare(String a,String b){
				String a_=a.split("column")[1];
				String b_=b.split("column")[1];
				return Integer.parseInt(a_)-Integer.parseInt(b_);
			}
		});
		for(int a=0;a<columnSize;a++){
			String column ="column";
			List list = new ArrayList();
			for(int k=3; k<listMerges.size(); k++){
				System.out.println(listMerges.get(k).toString());
				for(int x=0; x<listMerges.get(k).size(); x++){
					if(a==x){
						list.add(listMerges.get(k).get(x).getString("cellValue"));
					}
				}

			}
			map_data.put(column+a,list);
		}

		map_column.remove("column0");
		LinkedHashMap map_values_sp = new LinkedHashMap();
		Set<String> set = (Set<String>)map_column.keySet();

		List<String> set_sp = new ArrayList<>();
		for(String s:set){
			String suff_num = s.split("column")[1];
			int num = Integer.parseInt(suff_num);
			if(num % 2 == 1){
				set_sp.add(s);
			}
			if(num % 2 == 0){
				set_sp.add(s);
			}
		}

		for(String s:set_sp){
			String o =(String ) map_column.get(s);
			Map three = new HashMap();
			three.put("area",map_data.get("column0"));
			if(map_values_sp.containsKey(o)){
				three.put("point",((Map)map_values_sp.get(o)).get("point"));
				three.put("rank",map_data.get(s));

			}else{
				three.put("point",map_data.get(s));
			}
			map_values_sp.put(o,three);

		}

		DcEconomicIndicator dcEconomicIndicator = new DcEconomicIndicator();
		dcEconomicIndicator.setSelectMonth(selectMonth);
		dcEconomicIndicator.setSelectYear(selectYear);
		dcEconomicIndicator.setImportType("2");
		List<DcEconomicIndicator> list2 = dao.findList(dcEconomicIndicator);
		if(list2!=null && list2.size()>0){
			dcEconomicIndicator =list2.get(0);
		}else{
			dcEconomicIndicator.setId(null);
		}
		String jsonData = JSONObject.toJSONString(data);

		String jsonWhData = JSONObject.toJSONString(map_wh);
		String jsonColumnData = JSONObject.toJSONString(map_column);
		String jsonColumnTwoData = JSONObject.toJSONString(map_column_two);
		String jsonColumnDataChart3 = JSONObject.toJSONString(map_values_sp);
		dcEconomicIndicator.setJsonColumnData(jsonColumnData);
		dcEconomicIndicator.setJsonData(jsonData);
		dcEconomicIndicator.setJsonColumnTwoData(jsonColumnTwoData);
		dcEconomicIndicator.setJsonWhData(jsonWhData);
		dcEconomicIndicator.setJsonColumnDataChart(jsonColumnDataChart3);
		dcEconomicIndicator.setImportType("2");
		super.save(dcEconomicIndicator);
	}
}